﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.LIB;
using HYElectricSystemDiagram.DataStruct;

namespace HYElectricSystemDiagram.FormSet
{
	public partial class FormWiringInstructions : HYBaseForm
	{
		public FormWiringInstructions()
		{
			this.InitializeComponent();
			this.lst = GetXmlData.GetWiringInstructionsInfo(null);
			this.Refesh(this.lst);
		}

		private void Refesh(List<WiringInstructions> lst)
		{
			this.dataGridView1.DataSource = null;
			DataTable dataTable = new DataTable();
			dataTable.Columns.Add("      ", typeof(string));
			dataTable.Rows.Add(new object[]
			{
				"浪涌保护器型号"
			});
			dataTable.Rows.Add(new object[]
			{
				"二次接线说明"
			});
			dataTable.Rows.Add(new object[]
			{
				"二次接线说明"
			});
			dataTable.Rows.Add(new object[]
			{
				"二次接线说明"
			});
			dataTable.Rows.Add(new object[]
			{
				"二次接线说明"
			});
			dataTable.Rows.Add(new object[]
			{
				"控制电路方案号"
			});
			dataTable.Rows.Add(new object[]
			{
				"备注"
			});
			for (int i = 0; i < lst.Count; i++)
			{
				WiringInstructions wiringInstructions = lst[i];
				dataTable.Columns.Add(wiringInstructions.strTypeName, typeof(string));
				dataTable.Rows[0][wiringInstructions.strTypeName] = wiringInstructions.strSurge;
				for (int j = 0; j < 4; j++)
				{
					if (j < wiringInstructions.lstWiringInstructions.Count)
					{
						dataTable.Rows[j + 1][wiringInstructions.strTypeName] = wiringInstructions.lstWiringInstructions[j];
					}
					else
					{
						dataTable.Rows[j + 1][wiringInstructions.strTypeName] = "  ";
					}
				}
				dataTable.Rows[5][wiringInstructions.strTypeName] = wiringInstructions.strCircuitScheme;
				dataTable.Rows[6][wiringInstructions.strTypeName] = wiringInstructions.strRemarks;
			}
			this.dataGridView1.DataSource = dataTable;
			for (int k = 1; k < lst.Count + 1; k++)
			{
				this.dataGridView1.Columns[k].Width = 260;
			}
		}

		private void btnIn_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.InitialDirectory = "c:\\";
			openFileDialog.Filter = "Xml文件|*.xml|所有文件|*.*";
			openFileDialog.RestoreDirectory = true;
			openFileDialog.FilterIndex = 1;
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				List<WiringInstructions> wiringInstructionsInfo = GetXmlData.GetWiringInstructionsInfo(openFileDialog.FileNames[0]);
				if (wiringInstructionsInfo.Count > 0)
				{
					this.Refesh(wiringInstructionsInfo);
					return;
				}
				YJKMessageBox.Show("Xml格式不符合要求！");
			}
		}

		private void btnOut_Click(object sender, EventArgs e)
		{
			if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
			{
				string text = this.folderBrowserDialog1.SelectedPath;
				text += "\\SystemGraphInfo.xml";
				if (text.Contains("\\\\"))
				{
					text = text.Replace("\\\\", "\\");
				}
				if (text.Contains("SystemGraphInfo"))
				{
					text = text.Replace("SystemGraphInfo", "动力配电接线说明");
				}
				if (File.Exists(text))
				{
					File.Delete(text);
				}
				GetXmlData.WriteXml(text, this.lst, false);
				YJKMessageBox.Show("导出成功\n\r导入路径为:" + text);
			}
		}

		private void btnSubmit_Click(object sender, EventArgs e)
		{
			string text = Path.Combine(Product.WorkingLocation, "SystemGraphSet.xml");
			if (!File.Exists(text))
			{
				string text2 = "SystemGraphSet.xml";
				string sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\" + text2;
				string destFileName = Product.WorkingLocation + text2;
				File.Copy(sourceFileName, destFileName);
			}
			List<WiringInstructions> wiringInstructions = this.GetWiringInstructions();
			GetXmlData.WriteXml(text, wiringInstructions, true);
			YJKMessageBox.Show("应用到配置文件！");
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			string text = Path.Combine(Product.WorkingLocation, "SystemGraphSet.xml");
			if (!File.Exists(text))
			{
				string text2 = "SystemGraphSet.xml";
				string sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\" + text2;
				string destFileName = Product.WorkingLocation + text2;
				File.Copy(sourceFileName, destFileName);
			}
			List<WiringInstructions> wiringInstructions = this.GetWiringInstructions();
			GetXmlData.WriteXml(text, wiringInstructions, true);
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private List<WiringInstructions> GetWiringInstructions()
		{
			List<WiringInstructions> list = new List<WiringInstructions>();
			int count = this.dataGridView1.Columns.Count;
			for (int i = 1; i < count; i++)
			{
				list.Add(new WiringInstructions
				{
					strTypeName = this.dataGridView1.Columns[i].HeaderText,
					strSurge = this.dataGridView1[i, 0].Value.ToString(),
					lstWiringInstructions = new List<string>()
                    //lstWiringInstructions = 
					{
						this.dataGridView1[i, 1].Value.ToString(),
						this.dataGridView1[i, 2].Value.ToString(),
						this.dataGridView1[i, 3].Value.ToString(),
						this.dataGridView1[i, 4].Value.ToString()
					},
					strCircuitScheme = this.dataGridView1[i, 5].Value.ToString(),
					strRemarks = this.dataGridView1[i, 6].Value.ToString()
				});
			}
			return list;
		}

		private List<WiringInstructions> lst = new List<WiringInstructions>();
	}
}
